package edu.kit.csl.cratylus.extraction;

import java.util.HashMap;
import java.util.List;

import edu.kit.csl.cratylus.datatypes.Candidate;
import edu.kit.csl.cratylus.datatypes.Word;

/**
 * This merger selects the most frequent element from the set of candidates and 
 * propagates it as representative for the set.
 */
public class FrequencyMerger implements Merger {

	/* (non-Javadoc)
	 * @see edu.kit.csl.cratylus.extraction.Merger#merge(java.util.List)
	 */
	@Override
	public Word merge(List<Candidate> candidates) {
		HashMap<Word, Integer> freqs = new HashMap<Word, Integer>();
		Word bestWord = null;
		int bestFreq = 0;
		for (Candidate candidate : candidates) {
			Word word = candidate.getWord();
			int freq = freqs.containsKey(word) ?
					freqs.get(word) + 1 : 1;
			freqs.put(word, freq);
			if (freq > bestFreq) {
				bestFreq = freq;
				bestWord = word;
			}
		}
		return bestWord;
	}
}
